<?php

/**
  +----------------------------------------------------------
 * 通过分类cid生成筛选条件html 
  +----------------------------------------------------------
 * @param  [type] $cid     [分类id]
 * @param  [array] $get [属性字符串]
 * @return [type]          [html]
   +----------------------------------------------------------
 */
//加载工具类
function sendhongbaoto($arr) {
		global $_AR,$_RQ;
		$data['mch_id'] = $arr['mchid'];
		$data['mch_billno'] = $data['mch_id'].date("Ymd",time()).date("His",time()).rand(1111,9999);
		$data['nonce_str'] = createNoncestr();
		$data['re_openid'] = $arr['openid'];
		$data['wxappid'] = $arr['mch_appid'];
	    $data['nick_name'] = $arr['hbname'];
		$data['send_name'] = $arr['username']?$arr['username']:'匿名好友';
	 
		$data['total_amount'] = $arr['fee']*100;
		$data['min_value'] = $arr['fee']*100;
		$data['max_value'] = $arr['fee']*100;
		$data['total_num'] = 1;
		$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
		$data['act_name'] = $arr['hbname'];
		$data['remark'] = '感谢您对我们的支持。';
		$data['wishing'] = $arr['body'];
		
		if(!$data['re_openid']) 
		{
			$rearr['return_msg']='缺少用户openid';
			return $rearr;
		}
		$data['sign'] =getSign($data);
		
		$xml =arrayToXml($data);
		$url ="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
		$re = wxHttpsRequestPem($xml,$url);
		$rearr = xmlToArray($re);
		return $rearr;
}
//确认是否未读信息
function checksms($uid,$type){
	$msg = pdo_getall('jy_vote_notice_log', array('uid'=>$uid,'isread'=>0,'type'=>$type),array('id','msg','url'));
	if(!empty($msg)){
		if(count($msg)>1){
			$return = $msg[0];
		}else{
			$return = $msg[0];
		}
		pdo_update('jy_vote_notice_log', array('isread'=>1),array('uid'=>$uid,'id'=>$msg[0]['id']));
	}else{
		$return = false;
	}
	return $return;
}
//发送网页弹窗短信
function set_websms($setmsg,$setuid=0,$getuid=1,$url='') {
		$data = array(
				'uid'=>$getuid,
				'type'=>2,
				'url'=>$url,
				'msg'=>$setmsg,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_vote_notice_log', $data);
		//set_workerman($setmsg,$setuid,$getuid);
		return true;
}
function createNoncestr( $length = 32 ) {
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
		$str ="";
		for ( $i = 0; $i < $length; $i++ ) 
		{
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
		}
		return $str;
	}
	function getSign($Obj) {
		foreach ($Obj as $k => $v) 
		{
			$Parameters[$k] = $v;
		}
		ksort($Parameters);
		$String = formatBizQueryParaMap($Parameters, false);
		$setting = uni_setting();
		$String = $String."&key=".$setting['pay']['wechat_miyue'];
	//	exi($setting['pay']['wechat_miyue'],'error');
		$String = md5($String);
		$result_ = strtoupper($String);
		return $result_;
	}
	function formatBizQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v) 
		{
			if($urlencode) 
			{
				$v = urlencode($v);
			}
			$buff .= $k . "=" . $v . "&";
		}
		$reqPar;
		if (strlen($buff) > 0) 
		{
			$reqPar = substr($buff, 0, strlen($buff)-1);
		}
		return $reqPar;
	}
	function arrayToXml($arr) {
		$xml = "<xml>";
		foreach ($arr as $key=>$val) 
		{
			if (is_numeric($val)) 
			{
				$xml.="<".$key.">".$val."</".$key.">";
			}
			else $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
		}
		$xml.="</xml>";
		return $xml;
	}
	function wxHttpsRequestPem( $vars,$url, $second=30,$aHeader=array()) {
		//global $_W;
		$ch = curl_init();
		
		curl_setopt($ch,CURLOPT_TIMEOUT,$second);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		
		curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLCERT,ROOT_D.'/addons/jy_vote/cert/'.$_SESSION['uniacid'].'/apiclient_cert.pem');
		curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLKEY,ROOT_D.'/addons/jy_vote/cert/'.$_SESSION['uniacid'].'/apiclient_key.pem');
	
	
	
		if( count($aHeader) >= 1 )
		{
			curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
		}
		curl_setopt($ch,CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
		$data = curl_exec($ch);
		//exi($data,'error');
		if($data)
		{
			curl_close($ch);
			return $data;
		}
		else 
		{
			$error = curl_errno($ch);
			echo "call faild, errorCode:$error\n";
			curl_close($ch);
			return false;
		}
	} 
	function xmlToArray($xml) {
		$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $array_data;
	}
	 function make_coupon_card() {
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rand = $code[rand(0,25)]
        .strtoupper(dechex(date('m')))
        .date('d').substr(time(),-5)
        .substr(microtime(),2,5)
        .sprintf('%02d',rand(0,99));
    for(
        $a = md5( $rand, true ),
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
        $d = '',
        $f = 0;
        $f < 8;
        $g = ord( $a[ $f ] ),
        $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
        $f++
    );
    return $d;
} 
?>
